package com.ssh.api.action;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionSupport;
import com.ssh.common.log.LogFactory;
import com.ssh.common.log.LogName;
import com.ssh.common.util.JsonResult;
import com.ssh.entity.Employee;
import com.ssh.manager.EmployeeManager;
import com.ssh.manager.TaskProductEmployeeManager;
@Controller
@ParentPackage("client")
@Scope("prototype")
@Results(value={@Result(name ="login",type="json",params={"root","jr","excludeProperties","data.employee.position.del,data.employee.position.createDate,data.employee.employee.*,data.employee.createDate","excludeNullProperties","true"})})

public class EmployeeClientAction extends ActionSupport {
	private static final long serialVersionUID = 970773022382019911L;
	JsonResult jr=new JsonResult();
	private String userName;
	private String password;
	
	@Resource(name="employeeManagerImpl")
	private EmployeeManager employeeManagerImpl;
	
	@Resource(name="taskProductEmployeeManagerImpl")
	private TaskProductEmployeeManager taskProductEmployeeManagerImpl;
	
	public String  login() {
		jr.setMethod("login");
		try{
	    	jr.setCode("0");
	    	Employee employee=this.employeeManagerImpl.login(userName);
	    	if (employee==null) {
	    		jr.setCode("1");
	    		jr.setMessage("用户名不存在");
			}else{
				if(employee.getPassword().equals(password)){
		    		if (employee.getDel()==0) {
			    		jr.setMessage("登陆成功");	
			    		Map<String, Object> data=new HashMap<String, Object>();
			    		data.put("employee", employee);
				    	Map<String, Object> task=taskProductEmployeeManagerImpl.LastTask(employee.getId());
			    		if (task!=null) {
							data.put("task", task);
						}else{
							data.put("task", new HashMap<String, String>());
						}
				    	jr.setData(data);
					}else{
		    			jr.setCode("2");
			    		jr.setMessage("你的账户已经被删除");	
					}
					
				}else{
		    		jr.setCode("3");
		    		jr.setMessage("你输入的密码错误");		
				}
			}
		}catch (Exception e) {
			jr.setCode("-1");
			jr.setMessage(e.getMessage());
			LogFactory.getDefaultLog().error("登陆失败", LogName.API);
		}
		return "login";
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public JsonResult getJr() {
		return jr;
	}

	public void setJr(JsonResult jr) {
		this.jr = jr;
	}
	
	
}
